package com.wetao.lib_common.net.progress;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.wetao.lib_common.R;
import com.wetao.lib_common.utils.DialogXUtil;

import me.drakeet.materialdialog.MaterialDialog;


/**
 * Dialog的进度控制
 */
public class ProgressDialogHandler extends Handler {
    public static final int SHOW_PROGRESS_DIALOG = 1;
    public static final int DISMISS_PROGRESS_DIALOG = 2;

   // private SweetAlertDialog sad;
    private MaterialDialog materialDialog;
    private Context context;
    private boolean cancelable;
    private ProgressCancelListener mProgressCancelListener;
    private String tipText;

    public ProgressDialogHandler(Context context, ProgressCancelListener mProgressCancelListener, boolean cancelable) {
        super();
        this.context = context;
        this.mProgressCancelListener = mProgressCancelListener;
        this.cancelable = cancelable;
    }

    public ProgressDialogHandler(Context context, ProgressCancelListener mProgressCancelListener, boolean cancelable, String tipText) {
        super();
        this.context = context;
        this.mProgressCancelListener = mProgressCancelListener;
        this.cancelable = cancelable;
        this.tipText = tipText;
    }


    private void initProgressDialog() {
 /*       if (sad == null) {
            sad = new SweetAlertDialog(context);
            sad.changeAlertType(SweetAlertDialog.PROGRESS_TYPE);
            sad.setTitleText("正在加载...");
            sad.setCancelable(cancelable);

            if (cancelable) {
                sad.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        mProgressCancelListener.onCancelProgress();
                    }
                });
            }

            if (!sad.isShowing()) {
                sad.show();
            }
        }*/

        if(false) {
            DialogXUtil.requestDialog(tipText, cancelable, new DialogXUtil.OnRequestDialogXListen() {
                @Override
                public void onCancelProgress() {
                    if(cancelable) {
                        mProgressCancelListener.onCancelProgress();
                    }
                }
            });
            return;
        }
        if(materialDialog == null){
            materialDialog = new MaterialDialog(context);
         //   materialDialog.setBackground(getDrawable(R.drawable.round_board_bg_mm2))
         //   materialDialog.setBackgroundResource(R.drawable.round_board_bg_mm2);

            if(!TextUtils.isEmpty(tipText)) {
                View inflate = View.inflate(context, R.layout.dialog_wait, null);
                TextView tv = inflate.findViewById(R.id.tv_msg);
                tv.setText(tipText);
                materialDialog.setContentView(inflate);
            } else {
                materialDialog.setContentView(R.layout.dialog_wait);
            }

            materialDialog.setCanceledOnTouchOutside(cancelable);
            if(cancelable){
                materialDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        mProgressCancelListener.onCancelProgress();
                    }
                });
            }
            materialDialog.show();
        }

    }

    private void dismissProgressDialog() {
//        if (sad != null) {
//            sad.dismiss();
//            sad = null;
//        }

        if(false) {
            DialogXUtil.waitDialogDismiss();
            return;
        }
        if(materialDialog != null){
            materialDialog.dismiss();
            materialDialog = null;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_PROGRESS_DIALOG:
                initProgressDialog();
                break;
            case DISMISS_PROGRESS_DIALOG:
                dismissProgressDialog();
                break;
        }
    }
}
